package com.ruoyi.ruoyiyinzheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ruoyiyinzheng.domain.bo.StExperimentResultBo;
import com.ruoyi.ruoyiyinzheng.domain.vo.StExperimentResultVo;
import com.ruoyi.ruoyiyinzheng.domain.StExperimentResult;
import com.ruoyi.ruoyiyinzheng.mapper.StExperimentResultMapper;
import com.ruoyi.ruoyiyinzheng.service.IStExperimentResultService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 实验结果Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-26
 */
@RequiredArgsConstructor
@Service
public class StExperimentResultServiceImpl implements IStExperimentResultService {

    private final StExperimentResultMapper baseMapper;

    /**
     * 查询实验结果
     */
    @Override
    public StExperimentResultVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询实验结果列表
     */
    @Override
    public TableDataInfo<StExperimentResultVo> queryPageList(StExperimentResultBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StExperimentResult> lqw = buildQueryWrapper(bo);
        Page<StExperimentResultVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询实验结果列表
     */
    @Override
    public List<StExperimentResultVo> queryList(StExperimentResultBo bo) {
        LambdaQueryWrapper<StExperimentResult> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StExperimentResult> buildQueryWrapper(StExperimentResultBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StExperimentResult> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getExperimentNumber()), StExperimentResult::getExperimentNumber, bo.getExperimentNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getYarnTension()), StExperimentResult::getYarnTension, bo.getYarnTension());
        lqw.eq(StringUtils.isNotBlank(bo.getColorFastness()), StExperimentResult::getColorFastness, bo.getColorFastness());
        lqw.eq(StringUtils.isNotBlank(bo.getStainingRate()), StExperimentResult::getStainingRate, bo.getStainingRate());
        lqw.eq(bo.getReportId() != null, StExperimentResult::getReportId, bo.getReportId());
        lqw.eq(StringUtils.isNotBlank(bo.getAcceptable()), StExperimentResult::getAcceptable, bo.getAcceptable());
        return lqw;
    }

    /**
     * 新增实验结果
     */
    @Override
    public Boolean insertByBo(StExperimentResultBo bo) {
        StExperimentResult add = BeanUtil.toBean(bo, StExperimentResult.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改实验结果
     */
    @Override
    public Boolean updateByBo(StExperimentResultBo bo) {
        StExperimentResult update = BeanUtil.toBean(bo, StExperimentResult.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StExperimentResult entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除实验结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
